package com.service.client;


import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.DigestUtils;
import org.springframework.web.client.RestTemplate;

import com.service.model.ElinkDepartmentDTO;
import com.service.model.ThirdCertDTO;

public class HttpClient {

	public static void main(String[] args) {
		//verifyThirdCert();
		forward();
	}
	
	public static void forward() {
		RestTemplateBuilder build=new RestTemplateBuilder();
		RestTemplate restTemplate=build.build();
		String url="http://localhost:8088/api/dutyroom/forward/elink/userlist/query";
		
		HttpHeaders headers=new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		ElinkDepartmentDTO elinkDepartment=new ElinkDepartmentDTO();
		elinkDepartment.setId("123");
		elinkDepartment.setName("总部");
		headers.set("access-token", "eyJhbGciOiJIUzUxMiJ9.eyJ0aGlyZENlcnQiOiJTdXBlckFkbWluIiwiYWNjb3VudCI6IlN1cGVyQWRtaW4iLCJ1c2VySWQiOiJTdXBlckFkbWluIiwiZW1wbG95ZWVJZCI6IlN1cGVyQWRtaW4iLCJlbXBsb3llZU5hbWUiOiLotoXnuqfnrqHnkIblkZgiLCJvcmdJZCI6IjM4OThiZDI2ZDBiMDExZWFhNTdkMDAwYzI5MGRiNzUzIiwib3JnQ29kZSI6IjA2Iiwib3JnTmFtZSI6Iui0teW3nueUtee9keaciemZkOi0o-S7u-WFrOWPuCIsImJhc2VPcmdDb2RlIjoiMDYwMDAwIiwidGhpcmRTeXN0ZW1OYW1lIjoiZWxpbmsiLCJzYXBIck9yZ0lkIjoiQzk4NERDOUMyOTRBNDg2MDk2QzI1RkFBNzhDOTlBRUQiLCJzeXN0ZW1OYW1lIjoibnVsbCIsInN1YiI6Iui2hee6p-euoeeQhuWRmCIsImlhdCI6MTU5OTcwNDMyOSwiZXhwIjoxNTk5NzEwOTI5LCJyZWZyZXNoSW50ZXJ2YWwiOjExMCwianRpIjoiNDc5ODk4ZGQtZDUzNi00MjA1LTk0ODQtYTQ1ZWMyMDBiMGUwIn0.hXT5g41xRppswp0rJ_txBHghlFNx0C0L4fqP0oggscFaEAkmJJniRkLSAfdVjyRQpvSO4XBFI_NCRUYfWsNM8g");
		headers.set("token", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJjb2RlIjoie1widHlwZVwiOlwiZWxpbmtcIixcImNlcnRcIjpcIlN1cGVyQWRtaW5cIn0iLCJpZCI6LTE2MTcxODMyNTcsImp0aSI6ImM0YjIwMzlhOWI1ZDRmMDI5ZjEyMjNmNTAyOTlkZDExIn0.SCiZm4igCgsKvPu8umEXvmGxMfeaTx31o4-wcMyvEcE");
		
		long time=System.currentTimeMillis();
		String APP_KEY="eltKey";
		  StringBuffer temp = new StringBuffer();
	        temp.append("appKey=").append(APP_KEY).append("&")
	            .append("appSecret=").append("12345").append("&")
	            .append("timestamp=").append(time);
	        String signTemp = DigestUtils.md5DigestAsHex(temp.toString().getBytes());
		
	        //headers.set("appKey", APP_KEY);
			headers.set("appSecret", "12345");
			headers.set("timestamp", time+"");
			headers.set("sign", signTemp);
	        
	        HttpEntity<ElinkDepartmentDTO> requestEntity =new HttpEntity<ElinkDepartmentDTO>(elinkDepartment, headers);
    	//发送卡片消息
		String result=restTemplate.postForObject(url, requestEntity, String.class);
		System.out.println(result);
	}
	
	public  static void verifyThirdCert() {
		RestTemplateBuilder build=new RestTemplateBuilder();
		RestTemplate restTemplate=build.build();
		String url="http://localhost:8088/api/dutyroom/session/verifyThirdCert";
		long time=System.currentTimeMillis();
		String APP_KEY="eltKey";
		  StringBuffer temp = new StringBuffer();
	        temp.append("appKey=").append(APP_KEY).append("&")
	            .append("appSecret=").append("12345").append("&")
	            .append("timestamp=").append(time);
	        String signTemp = DigestUtils.md5DigestAsHex(temp.toString().getBytes());
		
		HttpHeaders headers=new HttpHeaders();
		headers.setContentType(MediaType.APPLICATION_JSON);
		headers.set("appKey", APP_KEY);
		headers.set("appSecret", "12345");
		headers.set("timestamp", time+"");
		headers.set("sign", signTemp);
		
		ThirdCertDTO thirdCert=new ThirdCertDTO();
		thirdCert.setType("elink");
		thirdCert.setCert("SuperAdmin");
		HttpEntity<ThirdCertDTO> requestEntity =new HttpEntity<ThirdCertDTO>(thirdCert, headers);
    	//发送卡片消息
		String result=restTemplate.postForObject(url, requestEntity, String.class);
		System.out.println(result);
	}
}
